package cn.hdustea.shopNavigation.ui.view.main

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.hdustea.shopNavigation.NavigationActivity
import cn.hdustea.shopNavigation.R
import cn.hdustea.shopNavigation.entity.Child
import cn.hdustea.shopNavigation.ui.contract.ChildContract
import cn.hdustea.shopNavigation.ui.presenter.ChildPresenter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hdustea.base.base.BaseActivity
import com.hdustea.base.widget.CustomLoadMoreView
import kotlinx.android.synthetic.main.activity_child.*
import kotlinx.android.synthetic.main.activity_search_result.*
import kotlinx.android.synthetic.main.item_list_amusement.*
import kotlinx.android.synthetic.main.item_list_amusement.view.*
import kotlinx.android.synthetic.main.item_list_child.view.*
import kotlinx.android.synthetic.main.item_list_child.view.rt_bar

/**
 * <pre>
 *     author : HuQiang
 *     time   : 2020/09/22
 *     desc   :
 *     version: 1.0
 * </pre>
 */
class ChildActivity : BaseActivity<ChildContract.Presenter>(), ChildContract.View {

    private var childAdapter: ChildAdapter? = null

    private var mCurrentPage: Int = 0

    companion object {

        fun startActivity(context: Context) {
            context.startActivity(Intent(context, ChildActivity::class.java))
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        child_search_button.setOnClickListener {
//            mPresenter?.getSearchList(child_search_button.text.toString())
//            setSearchList(childSearchList)
            Log.d("tag", "我点击了")

        }
    }

    override fun initView() {
        val layoutManager = LinearLayoutManager(this)
        child_recycler_view.layoutManager = layoutManager
        child_recycler_view.isNestedScrollingEnabled = true
        showLoading()
    }

    override fun initData() {
        mPresenter?.getChildList(0, false)
    }

    private fun initRecyclerView() {
        childAdapter!!.animationEnable = true
        childAdapter!!.loadMoreModule.loadMoreView = CustomLoadMoreView()
        childAdapter!!.loadMoreModule.setOnLoadMoreListener {
            mPresenter?.getChildList(mCurrentPage + 1, true)
        }
        childAdapter!!.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        childAdapter!!.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    private fun resetRecyclerView() {
        childAdapter!!.animationEnable = true
        childAdapter!!.loadMoreModule.loadMoreView = CustomLoadMoreView()

        childAdapter!!.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        childAdapter!!.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    override fun getLayoutRes(): Int = R.layout.activity_child

    override fun getPresenter(): ChildContract.Presenter = ChildPresenter(this)

    override fun setChildList(childList: MutableList<Child>, isRefresh: Boolean) {
        if (!isRefresh) {
            childAdapter = ChildAdapter(R.layout.item_list_child)
            initRecyclerView()
            child_recycler_view.adapter = childAdapter
            childAdapter!!.setList(childList)
        } else {
            if (childList.isNotEmpty()) {
                mCurrentPage++
                childAdapter!!.addData(childList)
                childAdapter!!.loadMoreModule.loadMoreComplete()
            } else {
                childAdapter!!.loadMoreModule.loadMoreEnd()
            }
        }
    }

    override fun setSearchList(childSearchList: MutableList<Child>) {
        childAdapter = ChildAdapter(R.layout.item_list_child)
        resetRecyclerView()
        search_result_view.adapter = childAdapter
        childAdapter!!.setList(childSearchList)
    }
    override fun onError() {
        childAdapter!!.loadMoreModule.isEnableLoadMore = true
        childAdapter!!.loadMoreModule.loadMoreFail()
    }

    inner class ChildAdapter(layoutRes: Int) :
            BaseQuickAdapter<Child, BaseViewHolder>(layoutRes), LoadMoreModule {

        private val OPTIONS: RequestOptions = RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .dontAnimate()

        override fun convert(holder: BaseViewHolder, item: Child) {
            val rootView = holder.itemView
            Glide.with(context).load(item.picurl).apply(OPTIONS).into(rootView.im_item_icon)
            rootView.tv_child_name.text = item.name
            rootView.tv_child_type.text = item.tag
            rootView.rt_bar.rating = item.stars
            if (item.commentnum == 0) {
                rootView.tv_child_comment_num.visibility = View.GONE
            } else {
                rootView.tv_child_comment_num.text = String.format(context.resources.getString(R.string.comments_num), item.commentnum)
                rootView.tv_child_comment_num.visibility = View.VISIBLE
            }
            if (item.consume_per == 0) {
                rootView.tv_child_average_price.visibility = View.GONE
            } else {
                rootView.tv_child_average_price.text = String.format(context.resources.getString(R.string.life_price), item.consume_per)
                rootView.tv_child_average_price.visibility = View.VISIBLE
            }
            rootView.tv_child_location.text = item.region
        }

    }

}